package com.school.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.school.entity.HljtClassimage;
import com.school.entity.HljtSchool;
import com.school.entity.HljtSchoolimage;
import com.school.entity.HljtSchoolnotify;
import com.school.entity.android.CampusGeneral;
import com.school.service.HljtCampusService;
import com.school.util.JsonUtil;
import com.school.util.PictureVideoUpload;
import com.school.util.RepUtil;
import com.school.util.WhetherNotToUpdate;
@Controller
public class HljtCampusController {
	
	@Autowired
	private HljtCampusService hljtCampusService;

	
	
	/***
	 * 校园简介 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/campusIntroduction")
	@ResponseBody
	public JSONObject campusIntroduction(HljtSchoolimage hljtSchoolimage , JsonUtil jsonUtil) {
		try {
			if(null !=hljtSchoolimage.getType()&& hljtSchoolimage.getType().equals("1")) {
				HljtSchool	temp = hljtCampusService.campusIntroduction(hljtSchoolimage);
				if(null != temp) {
					temp.setImagePath(WhetherNotToUpdate.getIp()+temp.getImagePath());
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"学校简介");
				}else {
					jsonUtil.setSchool("该学校没有简介");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	
	/**
	 * 校园环境 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/campusEnvironment")
	@ResponseBody
	public JSONObject campusEnvironment(HljtSchoolimage hljtSchoolimage , JsonUtil jsonUtil) {
		try {
			if(null !=hljtSchoolimage.getType() && hljtSchoolimage.getType().equals("2")) {

				List<CampusGeneral>	temp = hljtCampusService.campusEnvironment(hljtSchoolimage);

				if(null != temp && temp.size()>0) {
					for (CampusGeneral cam : temp) {
						cam.setPictureUrl(WhetherNotToUpdate.getIp()+cam.getPictureUrl());
					}
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"校园环境");
				}else {
					jsonUtil.setSchool("校园环境空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	
	/***
	 * 学校校徽 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/schoolBadge")
	@ResponseBody
	public JSONObject schoolBadge(HljtSchoolimage hljtSchoolimage , JsonUtil jsonUtil) {
		try {
			if(null !=hljtSchoolimage.getType() && hljtSchoolimage.getType().equals("4")) {

				List<CampusGeneral>	temp = hljtCampusService.campusEnvironment(hljtSchoolimage);
				if(null != temp && temp.size()>0) {
					for (CampusGeneral cam : temp) {
						cam.setPictureUrl(WhetherNotToUpdate.getIp()+cam.getPictureUrl());
					}
					jsonUtil.setSucceed(JSON.toJSONString(temp.get(0)) ,"校园校徽");
				}else {
					jsonUtil.setSchool("校园校徽空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	/***
	 * 校园荣誉 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/campusHonor")
	@ResponseBody
	public JSONObject campusHonor(HljtSchoolimage hljtSchoolimage , JsonUtil jsonUtil) {
		try {
			if(null !=hljtSchoolimage.getType() && hljtSchoolimage.getType().equals("3")) {

				List<CampusGeneral>	temp = hljtCampusService.campusEnvironment(hljtSchoolimage);

				if(null != temp && temp.size()>0) {
					
					for (CampusGeneral cam : temp) {
						cam.setPictureUrl(WhetherNotToUpdate.getIp()+cam.getPictureUrl());
					}
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"校园荣誉");
				}else {
					jsonUtil.setSchool("校园荣誉空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	
	/***
	 * 校园优秀班级 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/excellentSchoolClass")
	@ResponseBody
	public JSONObject excellentSchoolClass(HljtClassimage hljtClassimage , JsonUtil jsonUtil) {
		try {
			if(null !=hljtClassimage.getType() && hljtClassimage.getType().equals("0")) {
				hljtClassimage.setType("1");
				List<CampusGeneral>	temp = hljtCampusService.excellentSchoolClass(hljtClassimage);
				if(null != temp && temp.size()>0) {
					for (CampusGeneral campusGeneral : temp) {
						campusGeneral.setGradeClasses(campusGeneral.getGrade()+"("+campusGeneral.getClasses()+")");
						campusGeneral.setPictureUrl(WhetherNotToUpdate.getIp()+campusGeneral.getPictureUrl());
					}
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"校园优秀班级");
				}else {
					jsonUtil.setSchool("校园优秀班级空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	
	/***
	 * 校园宣传视屏 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/schoolVideo")
	@ResponseBody
	public JSONObject schoolVideo(HljtSchoolimage hljtSchoolimage , JsonUtil jsonUtil) {
		try {
			if(null !=hljtSchoolimage.getType() && hljtSchoolimage.getType().equals("5")) {

				List<CampusGeneral>	temp = hljtCampusService.campusEnvironment(hljtSchoolimage);

				if(null != temp && temp.size()>0) {
					for (CampusGeneral cam : temp) {
						cam.setPictureUrl(WhetherNotToUpdate.getIp()+cam.getPictureUrl());
					}
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"校园宣传视屏");
				}else {
					jsonUtil.setSchool("校园宣传视屏空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	/***
	 * 校园师资力量 
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/campusTeachersStrength")
	@ResponseBody
	public JSONObject campusTeachersStrength(JsonUtil jsonUtil) {
		try {
			List<CampusGeneral>temp = hljtCampusService.campusTeachersStrength();
				if(null != temp && temp.size()>0) {
					for (CampusGeneral cam : temp) {
						cam.setPictureUrl(WhetherNotToUpdate.getIp()+cam.getPictureUrl());
					}
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"校园师资力量");
				}else {
					jsonUtil.setSchool("校园师资力量空");
				}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
	}
	
	/***
	 * 校园通知 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/campusNotifications")
	@ResponseBody

	public JSONObject campusNotifications(HljtSchoolnotify hljtSchoolnotify ,JsonUtil jsonUtil) {
		try {
			if(null !=hljtSchoolnotify.getType() && hljtSchoolnotify.getType().equals("6")) {
				List<HljtSchoolnotify>temp = hljtCampusService.campusNotification(hljtSchoolnotify);
				if(null != temp && temp.size()>0) {
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"校园通知");
				}else {
					jsonUtil.setSchool("校园通知空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
	}
	
	
	/***
	 * 校园内新闻 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/campusNews")
	@ResponseBody
	public JSONObject campusNews(HljtSchoolnotify hljtSchoolnotify ,JsonUtil jsonUtil) {
		try {
			if(null !=hljtSchoolnotify.getType() && hljtSchoolnotify.getType().equals("1")) {
				hljtSchoolnotify.setTypeEx("2");
				List<HljtSchoolnotify>temp = hljtCampusService.campusNotifications(hljtSchoolnotify);
				if(null != temp && temp.size()>0) {
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"校园内新闻");
				}else {
					jsonUtil.setSchool("校园内新闻空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
	}
	
	
	/***
	 * 校园外新闻 参数type
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/ofCampusNews")
	@ResponseBody
	public JSONObject ofCampusNews(HljtSchoolnotify hljtSchoolnotify ,JsonUtil jsonUtil) {
		try {
			if(null !=hljtSchoolnotify.getType() && hljtSchoolnotify.getType().equals("3")) {
				hljtSchoolnotify.setTypeEx("4");
				List<HljtSchoolnotify>temp = hljtCampusService.campusNotifications(hljtSchoolnotify);
				if(null != temp && temp.size()>0) {
					jsonUtil.setSucceed(JSON.toJSONString(temp) ,"校园外新闻");
				}else {
					jsonUtil.setSchool("校园外新闻空");
				}
			}else {
				jsonUtil.setFailMsg("参数错误");
			}

			
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setFailMsg("服务器异常");
		}
		return jsonUtil.toJSONString();
		
	}
	
	
	
//web	
	
	/***
	 * 查询新闻咨询
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/newsConsultation")
	@ResponseBody
	public JSONObject newsConsultation(HttpServletResponse rep , HljtSchoolnotify hljtSchoolnotify ,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		RepUtil.repPake(rep);
		try {
			if(null !=hljtSchoolnotify && hljtSchoolnotify.getType().equals("1") || hljtSchoolnotify.getType().equals("2")) {
				if(hljtSchoolnotify.getType().equals("1")) {
					hljtSchoolnotify.setTypeEx("3");
				}else if(hljtSchoolnotify.getType().equals("2")){
					hljtSchoolnotify.setTypeEx("4");
				}
				List<HljtSchoolnotify>temp = hljtCampusService.campusNotifications(hljtSchoolnotify);
				List<HljtSchoolnotify>re = new ArrayList<>();
				if(null != temp && temp.size()>0) {
					if(hljtSchoolnotify.getPageSize()>=temp.size()) {
						re = temp;
					}else {
						Integer pageNum = (hljtSchoolnotify.getPageIndex()-1)*hljtSchoolnotify.getPageSize();
						if(pageNum+hljtSchoolnotify.getPageSize()>=temp.size()) {
							re = temp.subList(pageNum, temp.size());
						}else {
							re = temp.subList(pageNum, pageNum+hljtSchoolnotify.getPageSize());
						}
					}
				}
				if(null != re && re.size()>0) {
					ret.put("count", temp.size());
					ret.put("resources", re);
				}else {
					ret.put("resources", "新闻空");
				}
			}else {
				ret.put("parameterError", "参数错误");
				ret.put("parameter", hljtSchoolnotify);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
		
	}
	
	
	
	
	
	/***
	 * 添加新闻
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping("/AddNewsConsultation")
	@ResponseBody
	public JSONObject AddNewsConsultation(HljtSchoolnotify hljtSchoolnotify ,HttpServletResponse rep , HttpServletRequest request, JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		RepUtil.repPake(rep);
		try {
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
			 List<String>list = new ArrayList<>();
			 MultipartFile file1 = null;
			 MultipartFile file2 = null;
			 MultipartFile file3 = null;
			if (isMultipart){ 
			    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
			     file1 = multipartRequest.getFile("imgFile1");
			     file2 = multipartRequest.getFile("imgFile2");
			     file3 = multipartRequest.getFile("imgFile3");
			}
			if(hljtSchoolnotify.getType().equals("1") || hljtSchoolnotify.getType().equals("3")) {
				 if(!file1.isEmpty()) {
					    String imagePath1 = PictureVideoUpload.savePictureVideo(file1, WhetherNotToUpdate.getSaveDisk());
					    list.add(imagePath1);
				    }
				    if(!file2.isEmpty()) {
				    	 String imagePath2 = PictureVideoUpload.savePictureVideo(file2, WhetherNotToUpdate.getSaveDisk());
				    	 list.add(imagePath2);
				    }
				    if(!file3.isEmpty()) {
				    	String imagePath3 = PictureVideoUpload.savePictureVideo(file3, WhetherNotToUpdate.getSaveDisk());
				    	list.add(imagePath3); 
				    }
			}
			if(hljtSchoolnotify.getType().equals("2") || hljtSchoolnotify.getType().equals("4")) {
				list.add(hljtSchoolnotify.getImagePath());
			}
			
			if(null !=hljtSchoolnotify) {
				hljtSchoolnotify.setCreatetime(new Date());
			boolean b = hljtCampusService.addSchoolnotify(hljtSchoolnotify, list);
			ret.put("resources", b);
			}else {
				ret.put("parameterError", "参数错误");
				ret.put("parameter", hljtSchoolnotify);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
		
	}
	
	
	
	/***
	 * 删除新闻
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/deleteNewsConsultation")
	@ResponseBody
	public JSONObject deleteSchoolnotifyById(HttpServletResponse rep , HttpServletRequest request, Integer id ,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		RepUtil.repPake(rep);
		try {
			
		if(null !=id) {
			boolean b = hljtCampusService.deleteSchoolnotifyById(id);
			ret.put("resources", b);
			}else {
				ret.put("parameterError", "参数错误");
				ret.put("parameter", id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
		
	}
	
	
	
	
	
	/***
	 * 修改新闻
	 * @param hljtSchoolimage
	 * @param jsonUtil
	 * @return
	 */
	@RequestMapping("/updateNewsConsultation")
	@ResponseBody
	public JSONObject updateNewsConsultation(String data , HttpServletResponse rep , HttpServletRequest request,JsonUtil jsonUtil) {
		JSONObject ret=new JSONObject();
		RepUtil.repPake(rep);
		HljtSchoolnotify hljtSchoolnotify = JSON.parseObject(data, HljtSchoolnotify.class); 
		try {
			
		if(null !=hljtSchoolnotify) {
			boolean b = hljtCampusService.updateSchoolnotifyById(hljtSchoolnotify);
			ret.put("resources", b);
			}else {
				ret.put("parameterError", "参数错误");
				ret.put("parameter", hljtSchoolnotify);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.put("serverException", "服务器异常");
		}
		return ret;
		
	}
	/*public JSONObject getReturnValue(Object object , int code , String msg) {
		JSONObject j = new JSONObject();
		j.put("registerInformation", object);
		j.put("register", code);
		j.put("registerMsg", msg);
		return j;
	}*/
}
